package com.xinqi.modules.sys.document.controller.admin;

import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.document.dto.req.DocumentManagerCreateDTO;
import com.xinqi.modules.sys.document.dto.req.DocumentManagerQueryDTO;
import com.xinqi.modules.sys.document.dto.req.DocumentManagerTreeDTO;
import com.xinqi.modules.sys.document.dto.req.DocumentManagerUpdateDTO;
import com.xinqi.modules.sys.document.dto.rsp.DocumentManagerResultDTO;
import com.xinqi.modules.sys.document.dto.rsp.DocumentManagerTreeResultDTO;
import com.xinqi.modules.sys.document.service.DocumentManagerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 文档管理表Admin控制层
 *
 * @author: gzy
 * @date: 2023/05/28
 */
@Tag(name = "Admin-文档管理表", description = "文档管理表Admin接口")
@RestController
@RequestMapping("/admin/document-manager")
@RequiredArgsConstructor
public class DocumentManagerAdminController {
    private final DocumentManagerService documentManagerService;

    /**
     * 文档管理表-分页
     */
    @Operation(summary = "文档管理表-分页")
    @GetMapping("/page")
    public R<PageInfo<DocumentManagerResultDTO>> page(@ParameterObject DocumentManagerQueryDTO dto) {
        PageInfo<DocumentManagerResultDTO> page = documentManagerService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 文档管理表-详情
     */
    @Operation(summary = "文档管理表-详情")
    @GetMapping("/{id}")
    public R<DocumentManagerResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        DocumentManagerResultDTO dto = documentManagerService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增文档管理表
     */
    @Operation(summary = "新增文档管理表")
    @PostMapping
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增文档管理", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Long> insert(@RequestBody DocumentManagerCreateDTO dto) {
        return R.data(documentManagerService.insert(dto));
    }

    /**
     * 修改文档管理表
     */
    @Operation(summary = "修改文档管理表")
    @PutMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改文档管理", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody DocumentManagerUpdateDTO dto) {
        dto.setId(id);
        return R.data(documentManagerService.updateById(dto));
    }

    /**
     * 删除文档管理表
     */
    @Operation(summary = "删除文档管理表")
    @DeleteMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 删除文档管理", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(documentManagerService.deleteById(id));
    }

    @Operation(summary = "获取所有父级标题")
    @GetMapping("/findAllParentTitles/{parentId}")
    public R<List<DocumentManagerResultDTO>> findAllParentTitles(@Parameter(description = "parentId", example = "0") @PathVariable("parentId") Long parentId) {
        return R.data(documentManagerService.findAllByParentId(parentId));
    }

    @Operation(summary = "根据父级id获取子集列表")
    @GetMapping("/findChildren/{parentId}")
    public R<List<DocumentManagerResultDTO>> findChildren(@Parameter(description = "parentId", example = "0") @PathVariable("parentId") Long parentId) {
        return R.data(documentManagerService.findAllByParentId(parentId));
    }

    @Operation(summary = "获取文档管理树结构")
    @GetMapping("/findTree")
    public R<List<DocumentManagerTreeResultDTO>> findTree(@ParameterObject DocumentManagerTreeDTO dto) {
        return R.data(documentManagerService.findTree(dto));
    }

    @Operation(summary = "获取文档管理列表")
    @GetMapping("/findAll")
    public R<List<DocumentManagerResultDTO>> findAll(@ParameterObject DocumentManagerQueryDTO dto) {
        return R.data(documentManagerService.findAll(dto));
    }
}
